package com.myhome.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.myhome.domain.RolePrivilegesParam;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myhome.domain.SysRolePrivilege;
import com.myhome.mapper.SysRolePrivilegeMapper;
import com.myhome.service.SysRolePrivilegeService;

import java.util.ArrayList;
import java.util.List;

/**
*@Author QYiXin 
* @Date 2021 10 09 12 58
**/
@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService{

    @Override
    public boolean addPrivileges(RolePrivilegesParam rolePrivilegesParam) {
        Boolean result = true;
        Long roleId = rolePrivilegesParam.getRoleId();
        this.remove(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getRoleId, roleId));
        List<Long> privilegeIds = rolePrivilegesParam.getPrivilegeIds();
        if (!CollectionUtil.isEmpty(privilegeIds)) {
            List<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
            for (Long privilegeId : privilegeIds) {
                SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                sysRolePrivilege.setPrivilegeId(privilegeId);
                sysRolePrivilege.setRoleId(roleId);
                sysRolePrivileges.add(sysRolePrivilege);
            }
            result = this.saveBatch(sysRolePrivileges);
        }
        return result;
    }
}
